package com.workpaper.app.login.service;

import org.apache.ibatis.session.SqlSession;

import com.workpaper.app.db.mybatis.MyDB;
import com.workpaper.app.exceptions.BusinessException;
import com.workpaper.app.user.entity.User;
import com.workpaper.app.user.mapper.UserMapper;
/**
 * 登录服务
 * @author zhouyujun
 *
 */
public class LoginService {
	private static class Holder{
		private static LoginService INSTANCE = new LoginService();
	}
	public static LoginService getInstence() {
		return Holder.INSTANCE;
	}
	private LoginService() {
		super();
	}

	/**
	 * 登录操作
	 * @param userName
	 * @param password
	 * @return
	 */
	public boolean doLogin(String userName, String password) {
		try (SqlSession session = MyDB.openSession()){
			UserMapper userMapper = session.getMapper(UserMapper.class);
			User user = userMapper.selectByUserName(userName);
			if(user == null) {
				throw new BusinessException("用户名错误！");
			}
			if(password.equals(user.getPassword())) {
				//登录成功，缓存用户信息、权限信息
				return true;
			}
			throw new BusinessException("密码错误！");
		}catch (Exception e) {
			throw new BusinessException("登录异常：", e);
		}
	}
}
